package com.s4j.struts.biz.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.s4j.core.common.StringUtils;
import com.s4j.core.conset.Constant;
import com.s4j.core.datagrid.DatagridPaging;
import com.s4j.core.hibernate.CommonDao;
import com.s4j.struts.biz.model.BizLiAppr;
import com.s4j.struts.biz.model.BizLiCheck;
import com.s4j.struts.biz.model.BizLiCombin;
import com.s4j.struts.biz.model.BizLiCompute;
import com.s4j.struts.biz.model.BizLiMmgyz;
import com.s4j.struts.biz.model.BizLiOtherreson;
import com.s4j.struts.biz.model.BizLiReson;
import com.s4j.struts.biz.model.BizLiTreat;
import com.s4j.struts.biz.model.BizLiTreathis;
import com.s4j.struts.biz.model.PatientDoc;

@Service
public class LiverService {
	
//	private static Logger log = Logger.getLogger(LiverDocService.class);
	
	@Resource
	private CommonDao dao;

	public DatagridPaging findUserByPaging(Integer page, Integer rows,
			PatientDoc patientDoc) {
		List params = new ArrayList();
		String hql = "from PatientDoc su where su.diseaseType = ? ";
		params.add(Constant.PATIENTDOC_DISEASETYPE_LIVER);
		if(patientDoc != null && !StringUtils.isNullOrBlank(patientDoc.getName())) {
			hql += " and su.name like ? ";
			params.add('%' + patientDoc.getName() + '%');
		}
		params.add(page);
		params.add(rows);
		hql += " order by docId asc";
		DatagridPaging users = dao.findByHQLPaging(hql, params.toArray());
		return users;
	}

	public PatientDoc getPatientDoc(Long docId) {
		return dao.get(PatientDoc.class, docId);
	}
	
	public void saveCommon(PatientDoc patientDoc) {
		dao.save(patientDoc);
		patientDoc.setDocNo(String.valueOf(patientDoc.getDocId()));
		dao.update(patientDoc);
	}

	public void updateCommon(PatientDoc patientDoc) {
		dao.update(patientDoc);
	}
	
	public BizLiAppr getLiApprByPatientId(Long docId) {
		String hql = "from BizLiAppr t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizLiAppr.class, docId);
	}
	
	public BizLiCheck getLiCheckByPatientId(Long docId) {
		String hql = "from BizLiCheck t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizLiCheck.class, docId);
	}
	
	public BizLiCombin getLiCombinByPatientId(Long docId) {
		String hql = "from BizLiCombin t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizLiCombin.class, docId);
	}
	
	public BizLiCompute getLiComputeByPatientId(Long docId) {
		String hql = "from BizLiCompute t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizLiCompute.class, docId);
	}
	
	public BizLiMmgyz getLiMmgyzByPatientId(Long docId) {
		String hql = "from BizLiMmgyz t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizLiMmgyz.class, docId);
	}
	
	public BizLiOtherreson getLiOtherresonByPatientId(Long docId) {
		String hql = "from BizLiOtherreson t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizLiOtherreson.class, docId);
	}
	
	public BizLiReson getLiResonByPatientId(Long docId) {
		String hql = "from BizLiReson t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizLiReson.class, docId);
	}
	
	public BizLiTreat getLiTreatByPatientId(Long docId) {
		String hql = "from BizLiTreat t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizLiTreat.class, docId);
	}
	
	public BizLiTreathis getLiTreathisByPatientId(Long docId) {
		String hql = "from BizLiTreathis t where t.patient.docId = ?";
		return dao.findByHQLUnique(hql, BizLiTreathis.class, docId);
	}
	
	public void save(Object obj) {
		dao.save(obj);
	}
	
	public <T> T get(Class<T> clazz, Long id) {
		return dao.get(clazz, id);
	}

	public void update(Object obj) {
		dao.update(obj);
	}

	public void delete(Object obj) {
		dao.remove(obj);
	}

}
